#ifndef GAMEWIDGET_H
#define GAMEWIDGET_H

#include <QWidget>
#include <QGraphicsScene>
#include <QTimer>
#include <QKeyEvent>
#include <chrono>  //时间处理模块
#include "MapManager.h"
#include "PlayerCharacter.h"
#include "NPC.h"
#include "battle.h"

namespace Ui {
class GameWidget;
}

class GameWidget : public QWidget
{
    Q_OBJECT

public:
    explicit GameWidget(QWidget *parent = nullptr);
    ~GameWidget();

    QGraphicsScene *GetGameScene() { return gameScene; }
    // 游戏控制
    void startGame();
    void pauseGame();
    void resumeGame();
    void MapSwitch(const QString &name);
signals:
    void pauseRequested(); // 请求显示暂停菜单
    void wildBattle(QVector<Pokemon *> &playerPokemon);
    void npcBattle(QVector<Pokemon *>& playerPokemon,QVector<Pokemon *>&npcPokemon);

protected:
    void keyPressEvent(QKeyEvent *event) override;
    void keyReleaseEvent(QKeyEvent *event) override;




private slots:
    void updateGame(); // 游戏主循环


private:
    Ui::GameWidget *ui;
    QGraphicsScene *gameScene; // 游戏场景

    QTimer *gameTimer;      // 游戏循环计时器
    MapManager *mapManager; // 地图管理器
    PlayerCharacter *player; // 玩家
    QVector<NPC *> NpcArray; // 存储所有NPC

    // GameWidget.cpp - 添加安全清除场景的方法
    void safeClearScene();

    // 输入状态
    bool moveUp = false;
    bool moveDown = false;
    bool moveLeft = false;
    bool moveRight = false;
    bool isSprinting = false;

    // 时间管理
    std::chrono::high_resolution_clock::time_point lastUpdateTime;

    // 遭遇计时器
    float m_collidesTimer = 0.0f;
    const float COLLIDES_CHECK_INTERVAL = 0.5f;

    void initMap();
    void initPlayer();
    void initNpc();


    // 获取时间增量
    float getDeltaTime();
};

#endif // GAMEWIDGET_H
